/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.base.model.Partner;
import com.yunhoo.pay.bean.PayApplicationBean;
import com.yunhoo.pay.model.Expense;

import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class PayApplicationImpl {

    private static String nameSpace = "payApp.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static void insertAppBillVoucher(Expense exp) {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("affixAmount", exp.getAffixAmount().trim());
        map.put("memo", exp.getMemo());
        map.put("currency", exp.getCurrency().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        IbatisTemplate.insert(nameSpace + "insertAppBill", map);
    }

    public static void insertAppBillVoucher(SqlMapClient client, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("affixAmount", exp.getAffixAmount().trim());
        map.put("memo", exp.getMemo());
        map.put("currency", exp.getCurrency().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("customProjCode",exp.getCustomProjCode());
        client.insert(nameSpace + "insertAppBill", map);
    }

    public static void updateAppBill(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("affixAmount", exp.getAffixAmount().trim());
        map.put("currency", exp.getCurrency().getCode().trim());
        map.put("voutype", exp.getVoutype().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        IbatisTemplate.update(nameSpace + "updateAppBill", map);
    }

    public static void updateAppBill(SqlMapClient client, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("affixAmount", exp.getAffixAmount().trim());
        map.put("currency", exp.getCurrency().getCode().trim());
        map.put("voutype", exp.getVoutype().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        client.update(nameSpace + "updateAppBill", map);
    }

    public static void updatePartner(SqlMapClient client, Partner partner) throws SQLException {
        client.update(nameSpace + "updatePartner", partner);
    }

    public static void updatePartner(Partner partner) {
        IbatisTemplate.update(nameSpace + "updatePartner", partner);
    }

    public static void updatePayAppForCheck(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numstatus", exp.getNumberStatus());
        map.put("checkaccount", exp.getCheckaccount());
        map.put("remain", exp.getRemain());
        map.put("budgetsource", exp.getBudgetSource());
        map.put("eoc", exp.getCostitem().getCode());
        map.put("paycode", exp.getPaycode());
        map.put("costclass", exp.getCostClass());
        map.put("costtype", exp.getCostTypes().getCode());
        map.put("auditor", exp.getAuditor());
        map.put("auditdate", exp.getAuditDate());
        map.put("voutype", exp.getVoutype());
        IbatisTemplate.update(nameSpace + "updatePayAppForCheck", map);
    }

    public static Expense payAppByNumber(String number) {
        Map map = paramMap();
        map.put("number", number);
        return (Expense) IbatisTemplate.query(nameSpace + "payAppByNumber", map);
    }

    public static List appListByPerson(Map map) {
        return IbatisTemplate.queryForList(nameSpace + "appListByPerson", map);
    }

    public static List payAppListForCheck(Map map) {
        return IbatisTemplate.queryForList(nameSpace + "payAppListForCheck", map);
    }

    public static List payAppListForPay() {
        return IbatisTemplate.queryForList(nameSpace + "payAppListForPay", null);
    }

    public static List appBillListForPerson(Map map) {
        return IbatisTemplate.queryForList(nameSpace + "appBillListForPerson", map);
    }

    public static void insertPartner(Partner partner) {
        IbatisTemplate.insert(nameSpace + "addPartner", partner);
    }

    public static void insertPartner(SqlMapClient client, Partner partner) throws SQLException {
        client.insert(nameSpace + "addPartner", partner);
    }

    public static Partner partnerById(String id) {
        Map map = paramMap();
        map.put("id", id);
        return (Partner) IbatisTemplate.query(nameSpace + "partnerById", map);
    }

    public static void delApp(String number) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();

            Map map = paramMap();
            map.put("number", number);
            client.delete(nameSpace + "delPayAppByNumber", map);
            map.put("id", number);
            client.delete(nameSpace + "delPartner", map);

            client.commitTransaction();
        } catch (SQLException e) {
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(PayApplicationBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void submitApp(String number) {
        Map map = paramMap();
        map.put("number", number);
        IbatisTemplate.update(nameSpace + "submitApp", map);
    }

    public static void rescindApp(String number) {
        Map map = paramMap();
        map.put("number", number);
        IbatisTemplate.update(nameSpace + "rescindApp", map);
    }
}
